// miniprogram/pages/index/admin/managed_student/managed_student.js
const db = wx.cloud.database()
const _=db.command;
Page({
  
  /**
   * 页面的初始数据
   */
  data: {
    students: [],
    isBtn: true
  },
  importStudents: function(){
   
    const that=this;
    wx.chooseMessageFile({
      count: 1,
      type: 'file',
      extension: ['xlsx'],
      success: res=>{
        that.setData({isBtn: !that.data.isBtn})
        wx.showLoading({
          title: '导入中...',
        })
        const path=res.tempFiles[0].path;
        wx.cloud.uploadFile({
          cloudPath: "xlsx_temp/"+new Date().getTime()+'xlsx',
          filePath: path,
          success: res=>{
            const fileID=res.fileID;
            wx.cloud.callFunction({
              name: 'excel',
              data: {
                fileID: fileID
              },
              success: res=>{
                wx.hideLoading()
                getApp().tosSuccess("导入成功")
                that.setData({isBtn: !that.data.isBtn})
              }
            })
          }
        })
        
      }
    })
  },
  toDetailStudent: function(event){
    const sindex=event.target.dataset.sindex;
    const student=this.data.students[sindex];
    wx.navigateTo({
      url: 'detail_student/detail_student?student='+encodeURIComponent(JSON.stringify(student))
    })
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // const that=this;
    // db.collection("student").where({
    //   'student_obj.stu_type': _.gt(-100)
    // }).get().then(res=>{
    //   that.setData({students: res.data})
    // })
    this.loadData()
  },
  loadData: function(){
    const that=this;
    db.collection('student')
   .aggregate()
   .match({'student_obj.stu_type':_.gt(-100)})
   .sample({size: 20})
   .end().then(res=>{
     
     that.setData({students: res.list})
     
   })
 },
 onSearch: function(event){
   
  const value= event.detail.value

  const that=this;
  db.collection('student')
  .aggregate()
  .match({
    
    'student_obj.stu_type':_.gt(-100),
    student_obj: _.or([
      {
       stu_name: db.RegExp({'regexp':value,'options': "i"}),
      },
      { 
       stu_id: db.RegExp({'regexp': value,'options': "i"})
      }
    ])
   
  })
  .sample({size: 90})
  .end().then(res=>{
    
    that.setData({students: res.list})
    
  })
},
 onInput: function(event){
   
  const value=event.detail.value;
  if(value==""){
    this.loadData()
  }
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({isBtn: false})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
    this.loadData()
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})